//Hou
app.controller('addLinePreOrderController', ['$scope', 'lineCustomService', 'commodityService', 'ajax', '$state', '$stateParams', 'toolService','$filter', function ($scope, lineCustomService, commodityService, ajax, $state, $stateParams, toolService,$filter) {


    //获取明天的日期
    var getDate = new Date().getDate();
    var setDate=getDate+1;
    //3.0.toExponential();
    var getFullYear = new Date().getFullYear();
    var getMonth = new Date().getMonth() + 1;
    $scope.day = getFullYear + '-' + getMonth + '-' + setDate;


    //判断是更新数据还是添加数据
    if ($stateParams.id) {
        ajax.get({
            url: 'back/order/query',
            data: {
                orderId: $stateParams.id
            }
        }).then(function (result) {
            $scope.detail = result;
            ajax.get({
                url: 'back/order/orderDetailList',
                data: {
                    orderId: $stateParams.id
                },
                debug: true
            }).then(function (result) {

                $scope.detail.commoditys = result;
                result.forEach(function(item,index){
                    $scope.detail.commoditys[index].outPrice = item.unitPrice;
                });
            })
        })
    } else {
        $scope.commoditys = []
    }



    // 客户列表分页查询
    function lineCustomList(page) {
        lineCustomService.list({
            currentPage: page,
            pageSize: 10
        }).then(function (result) {
            $scope.linCustomerList = result;
        });
    }

    lineCustomList(1);
    //分页
    $scope.linCustomerPageChanged = function (e) {
        lineCustomList(e);
    };


    // 商品列表
    commodityService.list().then(function (result) {
        $scope.productList = result;
    });


    /*
     * 移出商品
     */
    $scope.removeCart = function (id) {
        $scope.detail.commoditys.forEach(function (item, index, array) {
            if (item.commodityId === id) {
                array.splice(index, 1);
            }
        })
    }

    //生成订单
    $scope.submit = function () {

        var ctime_tstart=$filter('date')($scope.detail.receivingDate,'yyyy-MM-dd');
        $scope.detail.receivingDate=ctime_tstart;
        if ($scope.detail.orderId) {
            var url = 'back/order/update'
        } else {
            var url = 'back/order/add';
        };
        ajax.post({
            data: $scope.detail,
            url: url,
            debug: true
        }).then(function (result) {
            $state.go('admin.linePreOrder')
        })
    }

}]);